package com.ea.admin.controller.company;

import com.ea.admin.permission.PermissionCode;
import com.ea.admin.pojo.Enum.Permission;
import com.ea.admin.pojo.param.company.AddCompanyParam;
import com.ea.admin.pojo.param.company.DeleteCompanyParam;
import com.ea.admin.pojo.param.company.EditCompanyParam;
import com.ea.admin.pojo.param.company.SearchCompanyContactParam;
import com.ea.admin.pojo.vo.account.ContactAdminVo;
import com.ea.admin.pojo.vo.company.CompanyVo;
import com.ea.admin.service.CompanyService;
import com.ea.admin.utils.NowUserGetter;
import com.ea.common.log.LogAnnotation;
import com.ea.common.result.Result;
import com.ea.common.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "单位管理-分公司管理")
@RequestMapping("/admin/company")
public class CompanyController {
    @Resource
    NowUserGetter nowUserGetter;
    @Resource
    CompanyService companyService;

    @Transactional
    @Operation(summary = "获得受管理的所有单位")
    @PostMapping("/getCompany")
    @LogAnnotation(module = "CompanyController", operation = "获得管理的单位")
    @Permission(permission_code = PermissionCode.COMPANY_MANAGE)
    public Result<List<CompanyVo>> getCompany() {
        List<CompanyVo> result = companyService.getCompanyPermissionById(
                nowUserGetter.getNowUser().getAccountId()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "联系人搜索")
    @PostMapping("/searchContact")
    @ApiImplicitParam(dataType = "SearchCompanyContactParam", name = "searchCompanyContactParam", value = "联系人搜索", readOnly = true, required = true)
    @LogAnnotation(module = "CompanyController", operation = "联系人搜索")
    @Permission(permission_code = PermissionCode.ADD_COMPANY)
    public Result<List<ContactAdminVo>> searchContact(@RequestBody @Valid SearchCompanyContactParam searchCompanyContactParam) {
        List<ContactAdminVo> result = companyService.searchContact(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(searchCompanyContactParam.getCompanyId()),
                searchCompanyContactParam.getContactName()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "添加新的单位")
    @PostMapping("/addCompany")
    @ApiImplicitParam(dataType = "AddCompanyParam", name = "addCompanyParam", value = "添加单位", readOnly = true, required = true)
    @LogAnnotation(module = "CompanyController", operation = "添加单位")
    @Permission(permission_code = PermissionCode.ADD_COMPANY,
            content = "添加单位“{companyName}”，设置默认管理员为“{companyContactUsername}”，姓名为“{companyContactName}”，手机号码为“{companyContactPhoneNum}”")
    public Result<String> addCompany(@RequestBody @Valid AddCompanyParam addCompanyParam) {
        Long data=companyService.addCompany(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(addCompanyParam.getSupCompanyId()),
                addCompanyParam.getCompanyName(),
                ParamUtil.parseId(addCompanyParam.getCompanyContactAccountId()),
                addCompanyParam.getCompanyContactName(),
                addCompanyParam.getCompanyContactPhoneNum(),
                addCompanyParam.getCompanyContactUsername(),
                addCompanyParam.getCompanyContactPassword(),
                addCompanyParam.getCompanyContactRePassword()
        );
        return Result.success(String.valueOf(data));
    }

    @Transactional
    @Operation(summary = "修改单位信息")
    @PostMapping("/editCompany")
    @ApiImplicitParam(dataType = "EditCompanyParam", name = "editCompanyParam", value = "修改单位", readOnly = true, required = true)
    @LogAnnotation(module = "CompanyController", operation = "修改单位")
    @Permission(permission_code = PermissionCode.EDIT_COMPANY,
            content = "修改单位“{companyName}”的信息")
    public Result editCompany(@RequestBody @Valid EditCompanyParam editCompanyParam) {
        companyService.editCompany(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(editCompanyParam.getCompanyId()),
                editCompanyParam.getCompanyName(),
                ParamUtil.parseId(editCompanyParam.getSupCompanyId()),
                ParamUtil.parseId(editCompanyParam.getCompanyContactId())
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "删除单位")
    @PostMapping("/deleteCompany")
    @ApiImplicitParam(dataType = "DeleteCompanyParam", name = "deleteCompanyParam", value = "删除单位", readOnly = true, required = true)
    @LogAnnotation(module = "CompanyController", operation = "删除单位")
    @Permission(permission_code = PermissionCode.DELETE_COMPANY,
            content = "删除单位“{companyName}”")
    public Result deleteCompany(@RequestBody @Valid DeleteCompanyParam deleteCompanyParam) {
        companyService.deleteCompany(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(deleteCompanyParam.getCompanyId())
        );
        return Result.success(null);
    }
}
